// 游戏状态
let gameState = {
    gold: 0,
    goldPerClick: 1,
    goldPerSecond: 0,
    upgrades: {}
};

// 升级配置
const upgradeConfig = [
    { id: 'cursor', name: '🖱️ 自动光标', desc: '每秒自动点击', baseCost: 15, baseBonus: 0.1, type: 'perSecond' },
    { id: 'grandma', name: '👵 金币奶奶', desc: '奶奶帮你赚钱', baseCost: 100, baseBonus: 1, type: 'perSecond' },
    { id: 'farm', name: '🌾 金币农场', desc: '种植金币', baseCost: 500, baseBonus: 5, type: 'perSecond' },
    { id: 'mine', name: '⛏️ 金币矿场', desc: '挖掘金币', baseCost: 2000, baseBonus: 20, type: 'perSecond' },
    { id: 'factory', name: '🏭 金币工厂', desc: '批量生产金币', baseCost: 10000, baseBonus: 100, type: 'perSecond' },
    { id: 'click1', name: '👆 强化点击', desc: '每次点击+1金币', baseCost: 50, baseBonus: 1, type: 'perClick' },
    { id: 'click2', name: '✌️ 双倍点击', desc: '每次点击+5金币', baseCost: 500, baseBonus: 5, type: 'perClick' },
    { id: 'click3', name: '🖐️ 超级点击', desc: '每次点击+25金币', baseCost: 5000, baseBonus: 25, type: 'perClick' },
];

// DOM 元素
const goldDisplay = document.getElementById('gold');
const perSecondDisplay = document.getElementById('per-second');
const perClickDisplay = document.getElementById('per-click');
const clickBtn = document.getElementById('click-btn');
const clickEffects = document.getElementById('click-effects');
const upgradeList = document.getElementById('upgrade-list');

// 格式化数字
function formatNumber(num) {
    if (num >= 1e12) return (num / 1e12).toFixed(2) + 'T';
    if (num >= 1e9) return (num / 1e9).toFixed(2) + 'B';
    if (num >= 1e6) return (num / 1e6).toFixed(2) + 'M';
    if (num >= 1e3) return (num / 1e3).toFixed(2) + 'K';
    return Math.floor(num).toString();
}

// 计算升级价格
function getUpgradeCost(upgrade) {
    const owned = gameState.upgrades[upgrade.id] || 0;
    return Math.floor(upgrade.baseCost * Math.pow(1.15, owned));
}

// 更新显示
function updateDisplay() {
    goldDisplay.textContent = formatNumber(gameState.gold);
    perSecondDisplay.textContent = formatNumber(gameState.goldPerSecond);
    perClickDisplay.textContent = formatNumber(gameState.goldPerClick);
    renderUpgrades();
}


// 渲染升级列表
function renderUpgrades() {
    upgradeList.innerHTML = upgradeConfig.map(upgrade => {
        const cost = getUpgradeCost(upgrade);
        const owned = gameState.upgrades[upgrade.id] || 0;
        const canAfford = gameState.gold >= cost;
        
        return `
            <div class="upgrade-item">
                <div class="upgrade-info">
                    <h3>${upgrade.name}</h3>
                    <p>${upgrade.desc} (+${upgrade.baseBonus}/个)</p>
                    <span class="owned">已拥有: ${owned}</span>
                </div>
                <button class="buy-btn" 
                    onclick="buyUpgrade('${upgrade.id}')"
                    ${canAfford ? '' : 'disabled'}>
                    💰 ${formatNumber(cost)}
                </button>
            </div>
        `;
    }).join('');
}

// 购买升级
function buyUpgrade(id) {
    const upgrade = upgradeConfig.find(u => u.id === id);
    const cost = getUpgradeCost(upgrade);
    
    if (gameState.gold >= cost) {
        gameState.gold -= cost;
        gameState.upgrades[id] = (gameState.upgrades[id] || 0) + 1;
        
        if (upgrade.type === 'perSecond') {
            gameState.goldPerSecond += upgrade.baseBonus;
        } else if (upgrade.type === 'perClick') {
            gameState.goldPerClick += upgrade.baseBonus;
        }
        
        updateDisplay();
    }
}

// 点击效果
function showClickEffect(e) {
    const rect = clickBtn.getBoundingClientRect();
    const x = e.clientX - rect.left + Math.random() * 40 - 20;
    const y = e.clientY - rect.top;
    
    const effect = document.createElement('div');
    effect.className = 'click-text';
    effect.textContent = '+' + formatNumber(gameState.goldPerClick);
    effect.style.left = x + 'px';
    effect.style.top = y + 'px';
    clickEffects.appendChild(effect);
    
    setTimeout(() => effect.remove(), 1000);
}

// 点击事件
clickBtn.addEventListener('click', (e) => {
    gameState.gold += gameState.goldPerClick;
    showClickEffect(e);
    updateDisplay();
});

// 每秒自动收益
setInterval(() => {
    if (gameState.goldPerSecond > 0) {
        gameState.gold += gameState.goldPerSecond / 10;
        updateDisplay();
    }
}, 100);

// 保存游戏
function saveGame() {
    localStorage.setItem('clickerGame', JSON.stringify(gameState));
    alert('游戏已保存！');
}

// 加载游戏
function loadGame() {
    const saved = localStorage.getItem('clickerGame');
    if (saved) {
        gameState = JSON.parse(saved);
    }
    updateDisplay();
}

// 重置游戏
function resetGame() {
    if (confirm('确定要重置游戏吗？所有进度将丢失！')) {
        localStorage.removeItem('clickerGame');
        gameState = {
            gold: 0,
            goldPerClick: 1,
            goldPerSecond: 0,
            upgrades: {}
        };
        updateDisplay();
    }
}

// 绑定按钮
document.getElementById('save-btn').addEventListener('click', saveGame);
document.getElementById('reset-btn').addEventListener('click', resetGame);

// 自动保存
setInterval(saveGame, 60000);

// 初始化
loadGame();
